package commonUtility;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
/**
  * Questa classe dovrà creare da tre file xml, per l'esattezza:
 * 0_transfer_job.xml
 * 1_transfer_job.xml
 * job.xml	
 * @author alex
**/

public class XmlMerge extends Thread{

	private String fileIn1 = null;
	private String fileIn2 = null;
	private String fileInOut = null;
	
	public XmlMerge(String fiout,String fi1,String fi2) {
		fileIn1=fi1;
		fileIn2=fi2;
		fileInOut=fiout;

	}	

	@SuppressWarnings("unchecked")
	public void mergeClient(){

		System.err.println("Chiamata le mergeClient con fileIn1="+fileIn1+", fileIn2="+fileIn2+", fileInOut="+fileInOut);
		
		SAXBuilder builder = new SAXBuilder();

		Document job_document = null;
		Document id_0_job_document = null;
		Document id_1_job_document = null;
		try {
			job_document = builder.build(new File(fileInOut));
			id_0_job_document = builder.build(new File(fileIn1));
			id_1_job_document  = builder.build(new File(fileIn2));

		} catch (JDOMException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		} 
		/**
		 * Carico la root del job.xml al quale andrò ad aggiungere i client
		 * presenti in id_0/1_job xml.
		 */
		Element job_root = job_document.getRootElement(); 
		/**
		 * Carico la root di id_0_job, per poi copiarne i client.
		 */
		Element id_0_root = id_0_job_document.getRootElement(); 
		List<Element> id_0_children = id_0_root.getChildren(); 
		Iterator<Element> id_0_iterator = id_0_children.iterator(); 
		while(id_0_iterator.hasNext()){ 
			Element item = (Element)id_0_iterator.next(); 
			String mac = item.getAttributeValue("MAC");	
			String file = item.getChildText("SOURCE");	
			String time = item.getChildText("TRANSMITTEDTIME");	
			String port = item.getChildText("PORT");		
			//Creo. 
			Element btClient = new Element("CLIENT"); 
			btClient.setAttribute("MAC",mac); 
			//Setto gli attibuti del figlio.
			Element source = new Element("SOURCE"); 
			Element timeValue = new Element("TRANSMITTEDTIME"); 
			Element portBt = new Element("PORT"); 
			source.setText(file);
			timeValue.setText(time);
			portBt.setText(port);
			btClient.addContent(source); 
			btClient.addContent(timeValue);
			btClient.addContent(portBt);	
			
			job_root.addContent(btClient);
		}
		/**
		 * Carico la root di id_1_job
		 */
		Element id_1_root = id_1_job_document.getRootElement(); 
		List<Element> id_1_children = id_1_root.getChildren(); 
		Iterator<Element> id_1_iterator = id_1_children.iterator(); 	
		while(id_1_iterator.hasNext()){ 
			Element item = (Element)id_1_iterator.next(); 
			String mac = item.getAttributeValue("MAC");	
			String file = item.getChildText("SOURCE");	
			String time = item.getChildText("TRANSMITTEDTIME");	
			String port = item.getChildText("PORT");		
			//Creo 
			Element btClient = new Element("CLIENT"); 
			btClient.setAttribute("MAC",mac); 
			Element source = new Element("SOURCE"); 
			Element timeValue = new Element("TRANSMITTEDTIME"); 
			Element portBt = new Element("PORT"); 
			source.setText(file);
			timeValue.setText(time);
			portBt.setText(port);
			btClient.addContent(source); 
			btClient.addContent(timeValue);
			btClient.addContent(portBt);	
			job_root.addContent(btClient);
		}
		/**
		 * Scrivo il file che ora avrà inseriti i job usurpati ai vicini.
		 */
		XMLOutputter outputter = new XMLOutputter(); 
		outputter.setFormat(Format.getPrettyFormat()); 
		try {
			outputter.output(job_document, new FileOutputStream(fileInOut));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}  
	
	public void run() {
		mergeClient();
	}
}
